<!--
  This is template for hugo menus, accepts Page and Menu as context
  https://gohugo.io/variables/menus/
-->
{{- if .Menu -}}
  {{- template "book-menu-hugo" . -}}
{{- end -}}

{{ define "book-menu-hugo" }}
<ul>
{{- range .Menu -}}
  {{- $class := slice -}}
  {{- with .Params.class -}}
    {{- $class = $class | append . -}}
  {{- end -}}
  {{- if $.Page.IsMenuCurrent .Menu . -}}
    {{- $class = $class | append "active" -}}
  {{- end -}}
  <li>
    {{- $isRemote := (urls.Parse .URL).IsAbs -}}
    <a href="{{ .URL }}"
      {{- with $class }} class="{{ delimit . " " }}"{{- end -}}
      {{- with .Title }} title="{{ . }}"{{- end -}}
      {{- if $isRemote }} target="_blank" rel="noopener"{{- end -}}>
      {{- .Pre -}}
      {{- template "book-menu-title" . -}}
      {{- .Post -}}
    </a>
    {{- with .Children -}}
      {{- template "book-menu-hugo" (dict "Page" $.Page "Menu" .) -}}
    {{- end -}}
  </li>
{{- end -}}
</ul>
{{ end }}

{{ define "book-menu-title" }}
{{- with .Params.BookIcon -}}
  <img src="{{ partial "docs/icon" . }}" class="book-icon" />
{{- end -}}
{{- if .Name -}}
  {{- .Name -}}
{{- else if .Page -}}
  {{- partial "docs/title" .Page -}}
{{- end -}}
{{ end }}